#include "common.h"

void write_to_flash(uint8 *fbuf, int flen, char *fname) {
    int fd = open(fname, O_RDWR | O_CREAT, 00777);
    if(fd < 0) {
        LOG_ERROR("open file error, error msg: %s", strerror(errno));
        return;
    }
    int rlen = write(fd, fbuf, flen);
    if(rlen == flen) {
        LOG_INFO("write file %s to flash successful", fname);
    } else {
        LOG_ERROR("#ERR error to write flash, flen: %d, code: %d", flen, rlen);
    }
}

void read_to_flash(uint8 *fbuf, int flen, char *fname) {
    int fd = open(fname, O_RDONLY, 00777);
    memset(fbuf, 0x0, flen);
    if(fd < 0) {
        LOG_WARN("file %s is not exist", fname);
    }
    int rlen = read(fd, fbuf, flen);
    if(rlen != flen) {
        LOG_ERROR("read file size is not %d, is: %d", flen, rlen);
    }
    LOG_INFO("read file %s to RAM successful", fname);
    // printHex1(fbuf, flen, 128*2, "rbuf");
}